﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <!-- 引用配置文件.-->
    <script src="../js/config.js" type="text/javascript"></script>

    <link href="../../../Scripts/easyUI/themes/default/easyui.css" rel="stylesheet" type="text/css" />
    <link href="../../../Scripts/easyUI/themes/icon.css" rel="stylesheet" type="text/css" />
    <script src="../../../Scripts/jquery-1.7.2.min.js" type="text/javascript"></script>
    <script src="../../../Scripts/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="../../../Scripts/easyUI/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
    <script src="../js/FormDesignerService.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">
        $(function () {
            var Ele_TBName = document.getElementById("TB_EnumName");
            Ele_TBName.focus();
            $('#TB_EnumName').blur(function () //文本失去焦点时触发的事件
            {
                var tbName = document.getElementById("TB_EnumName").value;
                var val = $('input:radio[name="RB_QPJP"]:checked').val();
                if (TB_Name == "") {
                    document.getElementById("TB_EnumKey").value = "";
                    return;
                }

                if (val == "ziduanquanpin") {
                    ParseStringToPinYin(tbName, true);                 
                }
                else {
                    ParseStringToPinYin(tbName, false);
                }

            });
            //点击全拼
            $('input[id=RB_QP]').change(function () {

                var tbName = document.getElementById("TB_EnumName").value; 
                ParseStringToPinYin(tbName, true);
            });
            //点击简拼
            $('input[id=RB_JP]').change(function () {
                var tbName = document.getElementById("TB_EnumName").value;
                ParseStringToPinYin(tbName, false);
            });
        });

        //转拼音，并去除开头为数字的字符
        function ParseStringToPinYin(parseString, parseModel) {
            parseString = $.trim(parseString);
            if (parseString != "") {
                $.post(controllerURLConfig, { action: 'ParseStringToPinyin', name: parseString, flag: parseModel }, function (data) {
                    if (data != "false") {
                        document.getElementById("TB_EnumKey").value = data;
                    }                        
                });
            }
        }

        //加载grid
        function LoadGridData(pageNumber, pageSize) {
            this.pageNumber = pageNumber;
            this.pageSize = pageSize;

            var params = {
                action: "GetEnumerationList",
                pageNumber: pageNumber,
                pageSize: pageSize
            };

            ajaxService(params, LoadDataGridCallBack, this);
        }

        //初始化
        $(function () {
            LoadGridData(1, 6);
        });

        //加载grid后回调函数
        function LoadDataGridCallBack(js, scorp) {
            $("#pageloading").hide();
            if (js == "") js = "[]";

            //系统错误
            if (js.status && js.status == 500) {
                $("body").html("<b>访问页面出错，请联系管理员。<b>");
                return;
            }
            var pushData = eval('(' + js + ')');

            $('#newsGrid').datagrid({
                columns: [[
                    { field: 'No', title: '编号', width: 80 },
                    { field: 'Name', title: '名称', width: 90 },
                    { field: 'CfgVal', title: '枚举值', width: 190 }
                ]],
                idField: 'No',
                singleSelect: true,
                data: pushData,
                width: 'auto',
                height: 'auto',
                striped: true,
                rownumbers: true,
                pagination: true,
                fitColumns: true,
                pageNumber: scorp.pageNumber,
                pageSize: scorp.pageSize,
                pageList: [6, 12, 18, 100],
                loadMsg: '数据加载中......',
                onClickRow: function (rowIndex, rowData) {
                    $('#TB_Name').val(rowData["Name"]);
                    $('#TB_KeyOfEn').val(rowData["No"]);
                    $('#TB_UIBindKey').val(rowData["No"]);
                    $('#TB_Vals').val(rowData["CfgVal"]);

                    //执行编辑函数.
                    Edit(false);
                }
            });
            //分页
            var pg = $("#newsGrid").datagrid("getPager");
            if (pg) {
                $(pg).pagination({
                    onRefresh: function (pageNumber, pageSize) {
                        LoadGridData(pageNumber, pageSize);
                    },
                    onSelectPage: function (pageNumber, pageSize) {
                        LoadGridData(pageNumber, pageSize);
                    }
                });
            }
        }

        //传入父窗体
        function GetFrmInfo() {
            return {
                Name: $.trim($("#TB_Name").val()),
                KeyOfEn: $.trim($("#TB_KeyOfEn").val()),
                IsCreateLabel: $("#CB_Generate_Tags").prop("checked"),
                UIBindKey: $.trim($("#TB_UIBindKey").val()),
                Vals: $.trim($("#TB_Vals").val())
            };
        }

        function NewEnum() {

            $("#TB_EnumName").val("");
            $("#TB_EnumKey").val("");
            $("#tabs").tabs('select', '枚举信息');

            var str = '<table>';
            for (var i = 1; i < 21; i++) {

                str += '<tr>';
                str += "<td> " + i + "</td> ";
                str += "<td> <input id='TB" + i + "' class='easyui-textbox' style='width: 200px' /></td> ";
                str += '</tr>';
            }

            var div = this.document.getElementById('DivEnumList');
            div.innerHTML = str;
        }

        function Del() {

            var name = $.trim($("#TB_Name").val());
            var key = $.trim($("#TB_KeyOfEn").val());

            if (key == null || key == "") {
                alert('您没有选择行.');
                return;
            }

            var v = window.confirm("您确定要删除枚举值[" + name + " " + key + "]吗？");
            if (v == false)
                return;

            alert('开始执行删除.');

            // 定义参数，让其保存到数据库里。
            var param = {
                action: "DoType",
                DoType: "DelEnum",
                EnumKey: key
            };

            $.post(controllerURLConfig, param, function (data) {
                if (data == "true") {
                    alert('删除成功.');
                    LoadGridData(1, 6);
                } else {
                    alert(data);
                }
            });
        }

        function Edit( isSetTab ) {

            var name =    $.trim($("#TB_Name").val());
            var key =     $.trim($("#TB_KeyOfEn").val());
            var cfgVal =  $.trim($("#TB_Vals").val());

            
            if (key == null || key == "") {
                alert('您没有选择行.');
                return;
            }

            $("#TB_EnumName").val(name);
            $("#TB_EnumKey").val(key);


            if (isSetTab) {
                $("#tabs").tabs('select', '枚举信息');
            }

            var cfgVals = cfgVal.split('@');
            var str = '<table>';
            var long = 0;
            for (var i = 0; i < 20; i++) {  
                      
                if (cfgVals[i] == '' || cfgVals[i] == null) {
                   
                     continue;
                   }
                   
                var vls = cfgVals[i].split('=');
                var text = vls[1];
                if(text == undefined){
                 continue;
                }
                str += '<tr>';
                str += "<td> " + i + "</td> ";
                str += "<td> <input id='TB" + i + "'  class='easyui-textbox'  value='"+text+"' class='easyui-textbox' style='width: 200px' /></td> ";
                str += '</tr>';
                long = i+1;
            }

            for (var i = long; i < 21; i++) {
             
                var text ='';
                str += '<tr>';
                str += "<td> " + i + "</td> ";
                str += "<td> <input id='TB" + i + "'  class='easyui-textbox'  value='" + text + "' class='easyui-textbox' style='width: 200px' /></td> ";
                str += '</tr>';
            }

            var div = this.document.getElementById('DivEnumList');
            div.innerHTML = str;
        }

        //执行保存.
        function SaveEnum() {

            var name =    $("#TB_EnumName").val();
            var keyofEn = $("#TB_EnumKey").val();

            if (name == '' || name == null) {
                alert('枚举中文名称不能为空.');
                return;
            }

            if (keyofEn == '' || keyofEn == null) {
                alert('枚举英文名称不能为空.');
                return;
            }

            var strs = '';
            for (var i = 0; i < 20; i++) {
                var v = $("#TB" + i).val();
                if (v == '' || v == null)
                    continue;
                strs += '@' + i + '=' + v;
            }

            //alert('将要保存' + strs + "  name=" + name + " keyofEn=" + keyofEn);

            // 定义参数，让其保存到数据库里。
            var param = {
                action: "DoType",
                DoType: "SaveEnum",
                EnumName: name,
                EnumKey: keyofEn,
                Vals: strs
            };

            $.post(controllerURLConfig, param, function (data) {
                if (data == "true") {
                    alert('保存成功,请在列表里选择该枚举.');
                    LoadGridData(1, 6);
                } else {
                    alert(data);
                }
            });

        }
    </script>
</head>
<body class="easyui-layout">
    <div data-options="region:'center'">
        <div id="tabs" class="easyui-tabs" data-options="fit:true ,border:false">
            <div title="选择已有枚举值" style="width: 600px; height: 600px;">
                <div id="tb" style="padding: 3px;">
                    <input type="button" id="Btn_New" class="easyui-button" onclick="NewEnum()" value="新增" />
                    &nbsp;&nbsp;
                    <input type="button" id="Btn_Edit" onclick="Edit(true)" value="编辑" />
                    &nbsp;&nbsp;
                    <input type="button" id="Btn_DELETE" onclick="Del()" value="删除" />
                </div>
                <table id="newsGrid" toolbar="#tb" class="easyui-datagrid">
                </table>

                <table>
                    <tr>
                        <td colspan="2">
                            <br />
                            中文名：
                            <input id="TB_Name" class="easyui-textbox" style="width: 150px" />
                            字段英文名：<input id="TB_KeyOfEn" class="easyui-textbox" style="width: 100px" />
                             <input id="CB_Generate_Tags" type="checkbox" checked="checked" /><label for="CB_Generate_Tags">生成标签</label>
                          
                           
                           

                          <input id="TB_UIBindKey" class="easyui-textbox" style="width: 200px; visibility:hidden" />
                          <input id="TB_Vals" class="easyui-textbox" style="width: 200px; visibility:hidden" />

                        </td>
                    </tr>
                </table>
            </div>

            <div title="枚举信息" id="tab_Edit" style="padding: 10px" >

           <table> 
           <tr>
           <td valign="top" >

                 <table>
                    <tr>
                      <td>  标签 </td>
                      <td> <input id="TB_EnumName" class="easyui-textbox" style="width: 200px" />  </td>
                    </tr>
                    <tr>
                      <td>  枚举键值 </td>
                      <td> <input id="TB_EnumKey" class="easyui-textbox" style="width: 200px" />  </td>
                    </tr>
                     
                     <tr>
                     <td colspan="2" >

                     <input id="RB_QP" type="radio" name="RB_QPJP" checked="checked" value="ziduanquanpin" /><label  for="RB_QP">字段全拼</label>
                     <input id="RB_JP" type="radio" name="RB_QPJP" value="ziduanjianpin" /><label for="RB_JP">字段简拼</label>

                     <input type="button"  value="重置"  onclick="NewEnum()" />
                     <input type="button"  value="保存"  onclick="SaveEnum()" />
                   <!--  <input type="button"  value="删除"  onclick="DelEnum()" />-->
                     
                     </td>
                     </tr>
                 </table>
                 </td>

                  <td valign="top">
                  <div  id='DivEnumList' ></div>
                  </td>
                 </tr>


                 </table>


            </div>
        </div>
    </div>
</body>
</html>
